<?php

namespace App\Http\Controllers\API;

use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;

class FormatController extends MYFroMatController
{

    private string $Lang;#语言 1维语|2汉语

    private array $User = [
        'status'=>false
    ];#用户信息
    private Request $Request;#请求
    private Array $Header;#请求头
    private int $Limit;

    private int $Page;

    public function __construct(Request $request){
        $this->Request = $request;
        $this->Header =  self::setDefault($request->header(),[]);
        $this->Limit =  self::setDefault($request->header('limit'),10);
        $this->Page =  self::setDefault($request->header('page'),1);
        $this->Lang =  self::setDefault($request->header('lang'),app()->getLocale());

        if(Auth::check()){
            $this->User = [
                'user'=>Auth::user(),
                'status'=>true,
                'type'=>'user'
            ];
        }
    }



    /**
     * 获取当前登录的用户数据
     * @return array|false[]
     */
    public function getUser(): array
    {
        return $this->User;
    }

    /**
     * 获取当前请求数据
     * @return Request
     */
    public function getRequest():Request
    {
        return $this->Request;
    }

    /**
     * 获取当请求的请求头数据
     * @return array|mixed
     */
    public function getHeader()
    {
        return $this->Header;
    }

    /**
     * 获取当前语言系统
     * @return string
     */
    public function getLang():string
    {
        return $this->Lang;
    }


    /**
     * 获取当前页面的页码
     * @return int
     */
    public function getPage(): int
    {
        return $this->Page;
    }

    /**
     * 获取当前页面数据数量 长度/条
     * @return int
     */
    public function getLimit(): int
    {
        return $this->Limit;
    }


    /**
     * 判断一个 $item 是否存在 则返回 $item否则返回 $default
     * @param $item
     * @param $default
     * @return mixed
     */
    private function setDefault($item,$default): mixed
    {
        if(!empty($item)){
            return $item;
        }
        return $default;
    }
}
